package br.com.portalaberto.telas;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;

import br.com.portalaberto.core.UIController;

public class EnviarNoticia extends GameCanvas{

	private LayerManager manager;

	private String msg;
	private Image image;
	private String pathCompleto;

	public static int width;
	public static int height;
	public static int X0CX;
	public static int Y0CX;
	public static int WCX;
	public static int HCX;
	public static int MEIOX;
	public static int MEIOY;
	public static int bordax;
	public static int borday;
	public static int MAX_TEXT_LENGTH = 26;

	private static EnviarNoticia instance;

	private Command sair;
	private Command escrever;
	private Command escolher;
	private Command enviar;

	private Graphics graphics;

	public static EnviarNoticia getInstance(UIController controlador){
		if (instance == null){
			instance = new EnviarNoticia(controlador, true);
		}
		return instance;
	}
	protected EnviarNoticia(UIController controlador, boolean suppressKeyEvents) {
		super(suppressKeyEvents);

		width = getWidth();
		height = getHeight();
		MEIOX = width/2;
		MEIOY = height/2;
		this.graphics = getGraphics();
		manager = new LayerManager();
		sair = new Command("Sair", Command.BACK, 0);
		escrever = new Command("Comentario", Command.OK, 1);
		escolher = new Command("Imagem", Command.OK, 1);
		enviar = new Command("Enviar", Command.OK, 1);
		this.addCommand(sair);
		this.addCommand(escrever);
		this.addCommand(escolher);
		this.addCommand(enviar);
		this.setCommandListener(controlador);

		limpaCaixa();
		desenhaString();
	}

	public void repinta(){
		desenhaString();
		desenhaImage();
	}

	private void limpaTela() {
		graphics.setColor(0x708090);//0x001B1432
		graphics.fillRect(0, 0, width, height);
		graphics.setColor(0x708090);
		//flushGraphics();
		//manager.paint(this.graphics, 0, 0);
	}

	private void limpaCaixa(){
		limpaTela();	
		desenhaCaixa();

		flushGraphics();
		manager.paint(this.graphics, 0, 0);
	}

	private void desenhaCaixa(){
		bordax = (int) (width * 0.05);
		borday = (int) (MEIOY * 0.1);

		int tamx = (int) (width * 0.9);
		int tamy = (int) (MEIOY * 0.85);

		graphics.setColor(0xffffff);
		graphics.fillRect(bordax, (MEIOY+borday), tamx, tamy);
		graphics.setColor(0x000000);
		graphics.drawRect(bordax, (MEIOY+borday), tamx, tamy);

	}

	public void desenhaString(){

		int cursorx = bordax+3;
		int cursory = MEIOY+borday+2;

		graphics.setColor(0x000000);
		if(msg != null && !"".equals(msg)){
			limpaCaixa();
			if(msg.length() < 27){
				msg = msg.toUpperCase();
				graphics.drawString ( msg, cursorx, cursory, Graphics.TOP | Graphics.LEFT);
			}else{
				String[] msgs = trataString(msg);
				for(int i=0; i<msgs.length; i++){
					if(i >= 6)
						break;
					graphics.drawString ( msgs[i], cursorx, cursory+(i*10), Graphics.TOP | Graphics.LEFT);
				}
			}
		}else{
			graphics.drawString ("ADICIONE UM COMENTARIO!", cursorx, cursory, Graphics.TOP | Graphics.LEFT);
		}
		flushGraphics();
		manager.paint(this.graphics, 0, 0);
	}

	private String[] trataString(String mens) {
		mens = mens.toUpperCase();
		String[] mens_cortada= null;
		if(mens.length() > 26){
			int num = (int)(mens.length() / 26);
			int num_mod = (mens.length() % 26);
			if(num_mod > 0){
				num++;
			}
			mens_cortada = new String[num];
			for(int i=0; i<num; i++){

				if(i == (num-1))
					mens_cortada[i] = mens.substring( (i*26), mens.length() );
				else
					mens_cortada[i] = mens.substring( (i*26), (i*26)+26 );
				System.out.println("indice["+i+"]= "+mens_cortada[i]);
			}
		}
		return mens_cortada;
	}

	public void desenhaImage() {
		if(image != null){
			int cursorx = (int) (width * 0.15);
			int cursory = (int) (height * 0.05);

			int tamMAX = (int) (width * 0.7);
			int altMAX = (int) (height * 0.4);

			Image i = resizeImage(image, tamMAX, altMAX);

			graphics.drawImage(i, cursorx, cursory, Graphics.TOP | Graphics.LEFT);
		}
		flushGraphics();
		manager.paint(this.graphics, 0, 0);

	}

	private Image resizeImage(Image src, int tamMAX, int altMAX) {
		int srcWidth = src.getWidth();
		int srcHeight = src.getHeight();
		Image tmp = Image.createImage(tamMAX, srcHeight);
		Graphics g = tmp.getGraphics();
		int ratio = (srcWidth << 16) / tamMAX;
		int pos = ratio/2;

		//Horizontal Resize        

		for (int x = 0; x < tamMAX; x++) {
			g.setClip(x, 0, 1, srcHeight);
			g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
			pos += ratio;
		}

		Image resizedImage = Image.createImage(tamMAX, altMAX);
		g = resizedImage.getGraphics();
		ratio = (srcHeight << 16) / altMAX;
		pos = ratio/2;        

		//Vertical resize

		for (int y = 0; y < altMAX; y++) {
			g.setClip(0, y, tamMAX, 1);
			g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
			pos += ratio;
		}
		return resizedImage;

	}//resize image

	public Command getBotaoSair(){
		return sair;
	}

	public Command getBotaoEscrever(){
		return escrever;
	}

	public Command getBotaoEscolher(){
		return escolher;
	}
	
	public Command getBotaoEnviar(){
		return enviar;
	}

	public String getMsg() {
		return msg;
	}
	
	public Image getImage() {
		return image;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public void setImage(Image image) {
		Image im = Image.createImage(image);

		this.image = im;
	}
	
	public String getPathCompleto() {
		return pathCompleto;
	}

	public void setPathCompleto(String pathCompleto) {
		this.pathCompleto = pathCompleto;
	}

}
